home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / c / qtools0.2-src.lha / src / libqtools / 3DS.c next >
Encoding:
C/C++ Source or Header  |  1998-07-18  |  3.1 KB  |  133 lines

  1. #define    LIBQTOOLS_CORE
  2. #include "../include/libqtools.h"
  3.  
  4. /* trilib.c: library for loading triangles from an Alias triangle file */
  5.  
  6. static struct triangle {
  7.   struct aliaspoint pt[3];
  8. };
  9.  
  10. static void ByteSwapTri(struct triangle *tri)
  11. {
  12.   int i;
  13.  
  14.   for (i = 0; i < sizeof(struct triangle) / 4; i++) {
  15.     ((int *)tri)[i] = BigLong(((int *)tri)[i]);
  16.   }
  17. }
  18.  
  19. bool LoadTriangleList(__memBase, char *fileName)
  20. {
  21.   HANDLE input;
  22.   float start;
  23.   char name[NAMEPATH_LEN], tex[256];
  24.   int i, count, magic;
  25.   struct triangle tri;
  26.   struct triangle *ptri;
  27.   int iLevel;
  28.   int exitpattern;
  29.   float t;
  30.   bool success;
  31.  
  32.   t = -FLOAT_START;
  33.   *((unsigned char *)&exitpattern + 0) = *((unsigned char *)&t + 3);
  34.   *((unsigned char *)&exitpattern + 1) = *((unsigned char *)&t + 2);
  35.   *((unsigned char *)&exitpattern + 2) = *((unsigned char *)&t + 1);
  36.   *((unsigned char *)&exitpattern + 3) = *((unsigned char *)&t + 0);
  37.  
  38.   if ((input = __open(fileName, H_READ_BINARY))) {
  39.     iLevel = 0;
  40.  
  41.     __read(input, &magic, sizeof(int));
  42.  
  43.     if (magic == BigLong(MAGIC_TRI)) {
  44.       AllocClusters(bspMem, TRIANGLES);
  45.       ptri = bspMem->triangles;
  46.  
  47.       while (__read(input, &start, sizeof(float))) {
  48.     *(int *)&start = BigLong(*(int *)&start);
  49.  
  50.     if (*(int *)&start != exitpattern) {
  51.       if (start == FLOAT_START) {
  52.         /* Start of an object or group of objects. */
  53.         i = -1;
  54.         do {
  55.           /*
  56.            * There are probably better ways to read a string from 
  57.            * a file, but this does allow you to do error checking 
  58.            * (which I'm not doing) on a per character basis.      
  59.            */
  60.           ++i;
  61.           __read(input, &(name[i]), sizeof(char));
  62.         } while (name[i] != '\0');
  63.  
  64.         __read(input, &count, sizeof(int));
  65.  
  66.         count = BigLong(count);
  67.         ++iLevel;
  68.         if (count != 0) {
  69.           i = -1;
  70.           do {
  71.         ++i;
  72.         __read(input, &(tex[i]), sizeof(char));
  73.           } while (tex[i] != '\0');
  74.         }
  75.         /*
  76.          * Else (count == 0) this is the start of a group, and 
  77.          * no texture name is present. 
  78.          */
  79.       }
  80.       else if (start == FLOAT_END) {
  81.         /*
  82.          * End of an object or group. Yes, the name should be 
  83.          * obvious from context, but it is in here just to be 
  84.          * safe and to provide a little extra information for 
  85.          * those who do not wish to write a recursive reader. 
  86.          * Mia culpa. 
  87.          */
  88.         --iLevel;
  89.         i = -1;
  90.         do {
  91.           ++i;
  92.           __read(input, &(name[i]), sizeof(char));
  93.         } while (name[i] != '\0');
  94.         continue;
  95.       }
  96.     }
  97.  
  98.     /*
  99.      * read the triangles
  100.      */
  101.     for (i = 0; i < count; ++i) {
  102.       int j;
  103.  
  104.       __read(input, &tri, sizeof(struct triangle));
  105.  
  106.       ByteSwapTri(&tri);
  107.       for (j = 0; j < 3; j++) {
  108.         int k;
  109.  
  110.         for (k = 0; k < 3; k++) {
  111.           ptri->verts[j][k] = tri.pt[j].p.v[k];
  112.         }
  113.       }
  114.  
  115.       if (bspMem->numtriangles == bspMem->max_numtriangles)
  116.         ExpandClusters(bspMem, TRIANGLES);
  117.       bspMem->numtriangles++;
  118.       ptri = bspMem->triangles + bspMem->numtriangles;
  119.     }
  120.       }
  121.  
  122.       __close(input);
  123.       success = TRUE;
  124.     }
  125.     else
  126.       eprintf("file %s is not a Alias object separated triangle file, magic number is wrong.\n", fileName);
  127.   }
  128.   else
  129.     eprintf(failed_fileopen, fileName);
  130.  
  131.   return success;
  132. }
  133.